<?php

require_once ($DOCUMENT_ROOT . '/includes/functions.php');

session_start ();

if (valid_user ())
{
	$inheader = "<script type='text/javascript' src='/js/bobcat.js'></script>";
	do_header ('Add an Instructor',$inheader);
	do_menu ();
	
	if ($submit)
	{
		// remove all the spaces for compatability
		$fname = remove_spaces ($fname);
		$lname = remove_spaces ($lname);
		
		db_connect ();
		
		// verify that the instructor is not already in the list
		$query = "SELECT * FROM ilist WHERE lName='$lname' AND fName='$fname'";
		$result = mysql_query ($query);
		
		if (mysql_num_rows($result) > 0)
		{
			echo "<p>failure: already in the list</p>";
			do_footer ();
			exit;
		}
		
		// get the department id number
		$query = "SELECT id FROM dlist WHERE department='$dept'";
		$result = mysql_query ($query);
		$dept = mysql_result ($result, 0);
	
		// insert the instructor into the list
		$query = "INSERT INTO ilist SET lName='$lname', 
										fName='$fname', 
										departmentID='$dept'";
		$result = mysql_query ($query) 
			or die('Query failed: ' . mysql_error());

		// get string associated with departmentID
		$query = "SELECT department FROM dlist WHERE id='$dept'";
		$result = mysql_query ($query);
		$dept = mysql_result ($result, 0);

		echo "$fname $lname entered into the $dept department\n";

		$id = mysql_insert_id ();
		echo "<p><a href='/write.php?$id'>Write a review for $fname $lname</a></p>";
		echo "<p><a href='$PHP_SELF'alt='another'>enter another</a></p>";
	}
	else
	{
?>

<p>Enter Name of New Instructor:</p>
<p>Double check the list before adding to make sure they are not in the list. Please capitalize properly.  ex. John Smith.</p>
<p><a target='facsearch' href='http://www.montana.edu/msuinfo/dx/bzn/fs/'>
	search msu database for information on an instructor</a></p>
<form method="post" action="<?php echo $PHP_SELF ?>" onSubmit="return verifyReview (this);">
	<p>Last name:<br />
	<input type="text" name="lname" size="20"></p>
	<p>First name:<br />
	<input type="text" name="fname" size="20"></p>
	<p>Department:<br />
<?php 
		echo "<select size='1' name='dept'>\n";
		echo "<option>select it</option>\n";
		db_connect ();
	
		$query = "SELECT * FROM dlist ORDER BY department";
		$result = mysql_query ($query);

		while ($row = mysql_fetch_array($result)) 
		{
		   	echo "<option value=\"$row[department]\">$row[department]</option>";
		}
	
		mysql_free_result ($result);
?>	
	</select></p>

	<p><input type="submit" value="Submit" name="submit"><input type="reset" value="Reset" name="reset"></p>
</form>

<?php
	}
	
	// list out teacher already in the list
	echo "<p> teachers already in list:<br>";

	db_connect ();
		
	$query = 'SELECT lName, fName FROM ilist ORDER BY lName';
	$result = mysql_query($query) 
		or die('Query failed: ' . mysql_error());

	// Print all names out by last name
	while ($line = mysql_fetch_array($result))
	{
		echo $line[lName].", ".$line[fName]."<br />";
	}

	// Free resultset
	mysql_free_result($result);
	
	do_footer ();
}
?>
